/**
 * 
 */
package im.jsmith.android.bloxbreaker;

import java.util.ArrayList;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.shape.modifier.ColorModifier;
import org.anddev.andengine.entity.shape.modifier.LoopShapeModifier;
import org.anddev.andengine.entity.shape.modifier.SequenceShapeModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.util.Vector2Pool;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.scoreninja.adapter.ScoreNinjaAdapter;

/**
 * @author Jordon Smith
 */
public class GameActivity extends BaseGameActivity implements GameConstants, IAccelerometerListener, 
															ContactListener, IOnAreaTouchListener {
	
	private Camera mCamera;
	private Scene mScene;
	private PhysicsWorld mPhysicsWorld;
	
	// Block
	private Texture mBlockTex;
	private TextureRegion mBlockTexReg;
	
	// Paddle
	private Texture mPaddleTex;
	private TextureRegion mPaddleTexReg;
	private Sprite mPaddleSprite;
	private Body mPaddleBody;
	
	// Ball
	private Texture mBallTex;
	private TextureRegion mBallTexReg;
	private Sprite mBallSprite;
	private Body mBallBody;
	
	// Walls
	private Body mTopBody;
	private Body mBottomBody;
	private Body mLeftBody;
	private Body mRightBody;
	
	// Paddle Buttons
	private Sprite mRightButton;
	private Sprite mLeftButton;
	
	// Text
	private Texture mFontTex;
	private Texture mFontBigTex;
	private Texture mFontBigTex2;
	private Font mFont;
	private Font mFontBig;
	private Font mFontBig2;
	private ChangeableText mScoreText;
	private ChangeableText mLifesText;
	private Text mGameOverText;
	private Text mSubmitScoreText;
	private Text mGoBackText;
	private Text mPauseGameText;
	private Text mLaunchBallText;
	private Text mPausedText;
	
	// Sounds
	private Sound mLaunchSound;
	private Sound mPaddleHitSound;
	private Sound mBlockHitSound;
	private Sound mDieSound;
	private Sound mGameOverSound;
	private Sound mSelectSound;
	
	// Game
	private boolean mGameRunning = false;
	private boolean mBallInPlay = false;
	private int mLifes = 3;
	private int mPaddleSensitivity = 11;
	private int mGameScore = 0;
	private int mRowTicker = 0;
	private float[] mRowTickerColor;

	private BlockPool mBlockPool;
	private ArrayList<Block> mBlocks;
	private ArrayList<float[]> mColorArray;
	private TimerHandler mTimerHandler;
	private float mRowTimer = 10;

	private ScoreNinjaAdapter mScoreNinjaAdapter;
	
	private boolean mUseAccelerometer;
	private Vector2 mSavedVelocity;
	
	@Override
	public Engine onLoadEngine() {
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera).setNeedsSound(true));
	}

	@Override
	public void onLoadResources() {
		enableAccelerometerSensor(this);
		
		mBlockTex = new Texture(64, 32, TextureOptions.DEFAULT);
		mBlockTexReg = TextureRegionFactory.createFromAsset(mBlockTex, this, "gfx/block.png", 0, 0);
		
		mPaddleTex = new Texture(128, 16, TextureOptions.DEFAULT);
		mPaddleTexReg = TextureRegionFactory.createFromAsset(mPaddleTex, this, "gfx/paddle.png", 0, 0);
		
		mBallTex = new Texture(16, 16, TextureOptions.DEFAULT);
		mBallTexReg = TextureRegionFactory.createFromAsset(mBallTex, this, "gfx/ball.png", 0, 0);
		
		mFontTex = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFont = new Font(mFontTex, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.WHITE);
		mFontBigTex = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFontBigTex2 = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFontBig = new Font(mFontBigTex, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 90, true, Color.WHITE);
		mFontBig2 = new Font(mFontBigTex2, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 90, true, Color.WHITE);
		mEngine.getFontManager().loadFonts(mFont, mFontBig, mFontBig2);
		mEngine.getTextureManager().loadTextures(mBlockTex, mPaddleTex, mBallTex, mFontTex, mFontBigTex, mFontBigTex2);
	}

	@Override
	public Scene onLoadScene() {
		mEngine.registerUpdateHandler(new FPSLogger());
		mScene = new Scene(2);
		
		mPhysicsWorld = new PhysicsWorld(new Vector2(0, 0), false);
		mPhysicsWorld.setContactListener(this);
		
		getPreferences();
		
		initWalls();
		initPaddle();
		initSounds();
		initColorArray();
	
		if (!mUseAccelerometer)
			initControls();
		
		mBlockPool = new BlockPool(mEngine, mPhysicsWorld, mBlockTexReg);
		mBlocks = new ArrayList<Block>();
		
		mScoreNinjaAdapter = new ScoreNinjaAdapter(this, "bloxbreaker", "");
		
		mScoreText = new ChangeableText(0, 9, mFont, "Score: " + mGameScore, "Score: XXXXXX".length());
		mLifesText = new ChangeableText(CAMERA_WIDTH - 108, 9, mFont, "Lifes: " + mLifes, "Lifes: X".length());
		mScene.getTopLayer().addEntity(mScoreText);
		mScene.getTopLayer().addEntity(mLifesText);
		
		mGameOverText = new Text(100, CAMERA_HEIGHT / 2 - 100, mFontBig, "GAME OVER");
		mSubmitScoreText = new Text(110, 250, mFont, "Submit Score");
		mGoBackText = new Text(480 , 250, mFont, "Go Back");
	
		mPausedText = new Text(190, CAMERA_HEIGHT / 2 - 100, mFontBig2, "PAUSED");
		mPausedText.setVisible(false);
		mScene.getTopLayer().addEntity(mPausedText);
		
		mPauseGameText = new Text(CAMERA_WIDTH / 2 - 110, 9, mFont, "PAUSE");
		mLaunchBallText = new Text(CAMERA_WIDTH / 2 + 12, 9, mFont, "LAUNCH");
		mScene.getTopLayer().addEntity(mPauseGameText);
		mScene.getTopLayer().addEntity(mLaunchBallText);
		mScene.registerTouchArea(mPauseGameText);
		mScene.registerTouchArea(mLaunchBallText);
		
		mScene.setBackground(new ColorBackground(0, 0, 0));
		mScene.registerUpdateHandler(mPhysicsWorld);
		mScene.setOnAreaTouchListener(this);
		
		return mScene;
	}

	@Override
	public void onLoadComplete() {
		mGameRunning = true;
		initBlocks();
	}
	
	@Override
	public void onAccelerometerChanged(AccelerometerData data) {
		if (mGameRunning) {
			Vector2 vec2 = Vector2Pool.obtain();
			
			if (mUseAccelerometer) {
				vec2.set(data.getY() * mPaddleSensitivity, 0);
				mPaddleBody.setLinearVelocity(vec2);
			}
			
			if (mBallInPlay) {
				vec2.x = mBallBody.getLinearVelocity().nor().x * 10;
				vec2.y = mBallBody.getLinearVelocity().nor().y * 10;
				mBallBody.setLinearVelocity(vec2);
			}
			Vector2Pool.recycle(vec2);
		}
	}
	
	@Override
	public void beginContact(Contact contact) {
		if (mBallInPlay) {
			// hit the bottom, ball lost
			if (contact.getFixtureB().getBody().equals(mBallBody) &&
					contact.getFixtureA().getBody().equals(mBottomBody)) {
				mLifes--;
				removeBall();
				mDieSound.play();
				if (mLifes >=0)
					mLifesText.setText("Lifes: " + mLifes);
				else 
					gameOver();
				return;
			}
			// hit the paddle
			else if (contact.getFixtureB().getBody().equals(mBallBody) &&
					contact.getFixtureA().getBody().equals(mPaddleBody)) {
				mPaddleHitSound.play();
				Vector2 vec2 = Vector2Pool.obtain(mBallBody.getLinearVelocity());
				vec2.y = 10;
				mBallBody.setLinearVelocity(vec2);
				return;
			}
			/* Hit a block -
			 * Removed here instead of in endContact to avoid ball getting stuck
			 * while the blocks are moving down */
			else if (contact.getFixtureB().getBody().equals(mBallBody)
					 && !contact.getFixtureA().getBody().equals(mLeftBody)
					 && !contact.getFixtureA().getBody().equals(mRightBody)
					 && !contact.getFixtureA().getBody().equals(mTopBody)) {
				final Block block = (Block)contact.getFixtureA().getBody().getUserData();
				mBlockHitSound.play();
				mGameScore++;
				mScoreText.setText("Score: " + mGameScore);
				mBlocks.remove(block);
				block.getSprite().setVisible(false);
				mBlockPool.recyclePoolItem(block);
				// bounce off
				Vector2 vec2 = mBallBody.getLinearVelocity();
				vec2.y = -vec2.y;
				mBallBody.setLinearVelocity(vec2);
			}
		}
	}

	@Override
	public void endContact(Contact contact) {
		// Adjust velocity, avoid ball moving at large angle
		if (mBallInPlay)
			mBallBody.setLinearVelocity(adjustVelocity(mBallBody.getLinearVelocity()));
	}
	
	@Override
	public boolean onAreaTouched(TouchEvent event, ITouchArea area, float x, float y) {
		if (area.equals(mSubmitScoreText) && event.getAction() == TouchEvent.ACTION_UP) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (mGameScore > 0)
						mScoreNinjaAdapter.show(mGameScore);
				}
			});
			mSelectSound.play();
		} else if (area.equals(mGoBackText) && event.getAction() == TouchEvent.ACTION_UP) {
			mSelectSound.play();
			finish();
		} else if (area.equals(mLaunchBallText) && event.getAction() == TouchEvent.ACTION_UP) {
			if (mGameRunning && !mBallInPlay && mLifes >= 0) {
				launchBall();
			}
		} else if (area.equals(mPauseGameText) && event.getAction() == TouchEvent.ACTION_UP) {
			mGameRunning = !mGameRunning;
			if (!mGameRunning) {
				mPausedText.setVisible(true);
				if (mBallInPlay) {
					mSavedVelocity = mBallBody.getLinearVelocity();
					Vector2 vec2 = Vector2Pool.obtain();
					vec2.set(0, 0);
					mBallBody.setLinearVelocity(vec2);
					Vector2Pool.recycle(vec2);
				}
			} else
			{
				mPausedText.setVisible(false);
				if (mBallInPlay)
					mBallBody.setLinearVelocity(mSavedVelocity);
			}
		} else if (!mUseAccelerometer && area.equals(mRightButton)) {
			Vector2 vec2 = mPaddleBody.getLinearVelocity();
			if (event.getAction() == TouchEvent.ACTION_DOWN) {
				vec2.set(20, 0);
			} else if (event.getAction() == TouchEvent.ACTION_UP) {
				vec2.set(0, 0);
			}
			mPaddleBody.setLinearVelocity(vec2);
		} else if (!mUseAccelerometer && area.equals(mLeftButton)) {
			Vector2 vec2 = mPaddleBody.getLinearVelocity();
			if (event.getAction() == TouchEvent.ACTION_DOWN) {
				vec2.set(-20, 0);
			} else if (event.getAction() == TouchEvent.ACTION_UP) {
				vec2.set(0, 0);
			}
			mPaddleBody.setLinearVelocity(vec2);
		}
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		mScoreNinjaAdapter.onActivityResult(requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void initWalls() {
		final Shape bottom = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
		final Shape top = new Rectangle(0, 40, CAMERA_WIDTH, 2);
		final Shape left = new Rectangle(0, 40, 2, CAMERA_HEIGHT - 40);
		final Shape right = new Rectangle(CAMERA_WIDTH - 2, 40, 2, CAMERA_HEIGHT - 40);

		final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		mBottomBody = PhysicsFactory.createBoxBody(mPhysicsWorld, bottom, BodyType.StaticBody, wallFixtureDef);
		mTopBody = PhysicsFactory.createBoxBody(mPhysicsWorld, top, BodyType.StaticBody, wallFixtureDef);
		mLeftBody = PhysicsFactory.createBoxBody(mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
		mRightBody = PhysicsFactory.createBoxBody(mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

		mScene.getBottomLayer().addEntity(bottom);
		mScene.getBottomLayer().addEntity(top);
		mScene.getBottomLayer().addEntity(left);
		mScene.getBottomLayer().addEntity(right);
	}
	
	private void initPaddle() {
		mPaddleSprite = new Sprite(CAMERA_WIDTH / 2 - mPaddleTexReg.getWidth() / 2, 
									CAMERA_HEIGHT - mPaddleTexReg.getHeight(), mPaddleTexReg);
		mPaddleSprite.setColor(0.5f, 0.1f, 0.5f);
		
		final FixtureDef paddleFixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		mPaddleBody = PhysicsFactory.createBoxBody(mPhysicsWorld, mPaddleSprite, BodyType.DynamicBody, paddleFixtureDef);
		mPaddleBody.setFixedRotation(true);
		mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mPaddleSprite, mPaddleBody, true, false, false, false));
		mScene.getTopLayer().addEntity(mPaddleSprite);
	}
	
	private void initSounds() {
		try {
			mLaunchSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/launch.wav");
			mPaddleHitSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/paddlehit.wav");
			mBlockHitSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/blockhit.wav");
			mDieSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/die.wav");
			mGameOverSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/gameover.wav");
			mSelectSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/select.wav");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void launchBall() {
		mBallSprite = new Sprite((mPaddleSprite.getX() + mPaddleSprite.getWidth() / 2)
						- mBallTexReg.getWidth() / 2, 
						mPaddleSprite.getY() - 30, mBallTexReg);
		final LoopShapeModifier shapeModifier = new LoopShapeModifier(new SequenceShapeModifier(
			new ColorModifier(2, 2.5f, 1.6f, 1.0f, 0.3f, 1.5f, 2.2f),
			new ColorModifier(2, 1.6f, 2.5f, 0.3f, 1.0f, 2.2f, 1.5f)));
		mBallSprite.addShapeModifier(shapeModifier);
		mBallSprite.setScale(1.2f);
		
		final FixtureDef ballFixtureDef = PhysicsFactory.createFixtureDef(0, 1, 0);
		mBallBody = PhysicsFactory.createCircleBody(mPhysicsWorld, mBallSprite, BodyType.DynamicBody, ballFixtureDef);
		mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mBallSprite, mBallBody, true, true, false, false));
		mScene.getTopLayer().addEntity(mBallSprite);
		
		Vector2 vec2 = Vector2Pool.obtain();
		if (Math.random() * 10 < 5)
			vec2.set(10, -10);
		else
			vec2.set(-10, 10);
		mBallBody.setLinearVelocity(vec2);
		Vector2Pool.recycle(vec2);
		
		mBallInPlay = true;
		mLaunchSound.play();
	}
	
	private void removeBall() {
		mEngine.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {
				mBallInPlay = false;
				PhysicsConnector connector = mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(mBallSprite);
				mPhysicsWorld.unregisterPhysicsConnector(connector);
				mPhysicsWorld.destroyBody(connector.getBody());
				mScene.getTopLayer().removeEntity(mBallSprite);
			}
		});
	}
	
	private void initBlocks() {
		float color[] = mColorArray.get((int)(Math.random() * (mColorArray.size() - 1)));
		addBlockRow(44, color[0], color[1], color[2]);
		moveRowsDown();
		addBlockRow(44, color[0], color[1], color[2]);
		moveRowsDown();
		moveRowsDown();
		moveRowsDown();
		color = mColorArray.get((int)(Math.random() * (mColorArray.size() - 1)));
		addBlockRow(44, color[0], color[1], color[2]);
		moveRowsDown();
		addBlockRow(44, color[0], color[1], color[2]);
		mTimerHandler = new TimerHandler(mRowTimer, new ITimerCallback() {
			@Override
			public void onTimePassed(TimerHandler arg0) {
				if (mRowTimer > 5)
					mTimerHandler.setTimerSeconds(mRowTimer = mRowTimer -0.5f);
				mTimerHandler.reset();
				if (!mGameRunning)
					return;
				switch (mRowTicker) {
				case 0:
					moveRowsDown();
					mRowTicker++;
					break;
				case 1:
					moveRowsDown();
					mRowTicker++;
					break;
				case 2:
					mRowTickerColor = mColorArray.get((int)(Math.random() * (mColorArray.size() - 1)));
					moveRowsDown();
					addBlockRow(44, mRowTickerColor[0], mRowTickerColor[1], mRowTickerColor[2]);
					mRowTicker++;
					break;
				case 3:
					moveRowsDown();
					addBlockRow(44, mRowTickerColor[0], mRowTickerColor[1], mRowTickerColor[2]);
					mRowTicker = 0;
					break;
				}	
			}
		});
		mEngine.registerUpdateHandler(mTimerHandler);
	}
	
	private void addBlockRow(float y, float r, float g, float b) {
		float x = 9.8f;
		Vector2 vec2 = Vector2Pool.obtain();
		for (int i = 0; i < 10; i++) {
			Block block = mBlockPool.obtainPoolItem();
			block.getSprite().setColor(r, g, b);
			
			// set position
			vec2.x = pixelToMeter(x + block.getSprite().getWidth() / 2);
			vec2.y = pixelToMeter(y + block.getSprite().getHeight() / 2);
			block.getBody().setTransform(vec2, 0);
			x += 70.8f;
			mBlocks.add(block);
		}
		Vector2Pool.recycle(vec2);
	}
	
	private void moveRowsDown() {
		for (int i = 0; i < mBlocks.size(); i++) {
			final Block block = mBlocks.get(i);
			// check we havn't lost the game
			if (block.getSprite().getY() + 38 > CAMERA_HEIGHT - 68) {
				gameOver();
				if (mBallInPlay)
					removeBall();
				return;
			}
			// reposition
			Vector2 vec2 = block.getBody().getPosition();
			vec2.y += pixelToMeter(38.8f);
			block.getBody().setTransform(vec2, 0);
		}
	}
	
	private float pixelToMeter(float pixel) {
		return pixel / PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
	}
	
	private void gameOver() {
		mGameRunning = false;
		mGameOverSound.play();
		mScene.getTopLayer().addEntity(mGameOverText);
		mScene.getTopLayer().addEntity(mSubmitScoreText);
		mScene.getTopLayer().addEntity(mGoBackText);
		mScene.registerTouchArea(mSubmitScoreText);
		mScene.registerTouchArea(mGoBackText);
	}
	
	private void initColorArray() {
		mColorArray = new ArrayList<float[]>();
		// Blue
		float blue[] = {0.1f, 0.1f, 1.2f};
		mColorArray.add(blue);
		// Purple
		float purple[] = {0.5f, 0.1f, 0.5f};
		mColorArray.add(purple);
		// Yellow
		float yellow[] = {1.3f, 1.3f, 0};
		mColorArray.add(yellow);
		// Pink
		float pink[] = {2, 1.0f, 1.5f};
		mColorArray.add(pink);
		// Green
		float green[] = {0, 1.2f,0};
		mColorArray.add(green);
		// Red
		float red[] = {1.1f, 0, 0};
		mColorArray.add(red);
		// Orange
		float orange[] = {1.4f, 0.9f, 0};
		mColorArray.add(orange);
	}
	
    private void getPreferences() {
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    	mPaddleSensitivity = Integer.parseInt(prefs.getString("paddleSensitivity", "8"));
    	mUseAccelerometer = prefs.getBoolean("useAccelerometer", true);
    }
    
    private Vector2 adjustVelocity(Vector2 velocity) {
    	if (velocity.x < 0)
    		velocity.x = -10;
    	else
    		velocity.x = 10;
    	
    	if (velocity.y < 0)
    		velocity.y = -10;
    	else
    		velocity.y = 10;
    	
    	return velocity;
    }
    
    private void initControls() {
    	Texture rightTex = new Texture(64, 64, TextureOptions.DEFAULT);
    	TextureRegion rightTexReg = TextureRegionFactory.createFromAsset(rightTex, this, "gfx/right.png", 0, 0);
    	
    	Texture leftTex = new Texture(64, 64, TextureOptions.DEFAULT);
    	TextureRegion leftTexReg = TextureRegionFactory.createFromAsset(leftTex, this, "gfx/left.png", 0, 0);

    	mEngine.getTextureManager().loadTextures(rightTex, leftTex);
    	
    	mRightButton = new Sprite(CAMERA_WIDTH - 80, CAMERA_HEIGHT - 100, rightTexReg);
    	mLeftButton = new Sprite(16, CAMERA_HEIGHT - 100, leftTexReg);
    	
    	mScene.getTopLayer().addEntity(mRightButton);
    	mScene.getTopLayer().addEntity(mLeftButton);
    	mScene.registerTouchArea(mRightButton);
    	mScene.registerTouchArea(mLeftButton);
    }
}
